#include "ui_manager.h"
#include "idm_window.h"
#include "theme_manager.h"
#include <QApplication>
#include <QMessageBox>
#include <QHeaderView>
#include <QTableWidgetItem>
#include <QIcon>
#include <QPixmap>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QProgressBar>
#include <QCloseEvent>
#include <QDesktopServices>
#include <QUrl>
#include <QSplitter>
#include <QLineEdit>
#include <QTreeWidget>
#include <QTabWidget>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QTextEdit>
#include <QSpinBox>
#include <QCheckBox>
#include <QComboBox>
#include <QDateTime>
#include <QMenu>
#include <QContextMenuEvent>
#include <QPoint>
#include <QFrame>
#include <QStyle>

UIManager::UIManager(IDMWindow *parentWindow, QObject *parent)
    : QObject(parent)
    , m_parentWindow(parentWindow)
    , m_categoryPanelVisible(true)
    , m_menuBar(nullptr)
    , m_mainToolBar(nullptr)
    , m_tabWidget(nullptr)
    , m_downloadList(nullptr)
    , m_categoryTree(nullptr)
    , m_categoryPanel(nullptr)
    , m_mainStatusBar(nullptr)
    , m_trayIcon(nullptr)
    , m_searchBox(nullptr)
    , m_filterComboBox(nullptr)
{
}

UIManager::~UIManager()
{
}

void UIManager::createMenus()
{
    // 创建菜单栏
    m_menuBar = new QMenuBar(m_parentWindow);
    m_parentWindow->setMenuBar(m_menuBar);

    // 文件菜单
    m_fileMenu = m_menuBar->addMenu(QMainWindow::tr("&File"));
    m_newDownloadAction = m_fileMenu->addAction(QMainWindow::tr("&New Download..."));
    m_newDownloadAction->setShortcut(QMainWindow::tr("Ctrl+N"));
    m_newDownloadAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_FileIcon));
    m_parentWindow->connect(m_newDownloadAction, &QAction::triggered, m_parentWindow, &IDMWindow::newDownload);

    m_batchDownloadAction = m_fileMenu->addAction(QMainWindow::tr("&Batch Download..."));
    m_batchDownloadAction->setShortcut(QMainWindow::tr("Ctrl+B"));
    m_parentWindow->connect(m_batchDownloadAction, &QAction::triggered, m_parentWindow, &IDMWindow::batchDownload);

    m_fileMenu->addSeparator();
    
    m_pauseAction = m_fileMenu->addAction(QMainWindow::tr("&Pause"));
    m_parentWindow->connect(m_pauseAction, &QAction::triggered, m_parentWindow, &IDMWindow::pauseDownload);
    
    m_resumeAction = m_fileMenu->addAction(QMainWindow::tr("&Resume"));
    m_parentWindow->connect(m_resumeAction, &QAction::triggered, m_parentWindow, &IDMWindow::resumeDownload);
    
    m_cancelAction = m_fileMenu->addAction(QMainWindow::tr("&Cancel"));
    m_parentWindow->connect(m_cancelAction, &QAction::triggered, m_parentWindow, &IDMWindow::cancelDownload);
    
    m_deleteAction = m_fileMenu->addAction(QMainWindow::tr("&Delete"));
    m_parentWindow->connect(m_deleteAction, &QAction::triggered, m_parentWindow, &IDMWindow::deleteDownload);
    
    m_fileMenu->addSeparator();
    
    m_propertiesAction = m_fileMenu->addAction(QMainWindow::tr("P&roperties"));
    m_parentWindow->connect(m_propertiesAction, &QAction::triggered, m_parentWindow, &IDMWindow::properties);
    
    m_fileMenu->addSeparator();
    
    m_exitAction = m_fileMenu->addAction(QMainWindow::tr("E&xit"));
    m_exitAction->setShortcut(QMainWindow::tr("Alt+F4"));
    m_parentWindow->connect(m_exitAction, &QAction::triggered, m_parentWindow, &IDMWindow::exitApplication);

    // 下载菜单
    m_downloadMenu = m_menuBar->addMenu(QMainWindow::tr("&Download"));
    m_addUrlAction = m_downloadMenu->addAction(QMainWindow::tr("&Add URL..."));
    m_parentWindow->connect(m_addUrlAction, &QAction::triggered, m_parentWindow, &IDMWindow::addUrl);
    
    m_downloadMenu->addSeparator();
    
    m_pauseSelectedAction = m_downloadMenu->addAction(QMainWindow::tr("P&ause Selected"));
    m_parentWindow->connect(m_pauseSelectedAction, &QAction::triggered, m_parentWindow, &IDMWindow::pauseSelected);
    
    m_resumeSelectedAction = m_downloadMenu->addAction(QMainWindow::tr("&Resume Selected"));
    m_parentWindow->connect(m_resumeSelectedAction, &QAction::triggered, m_parentWindow, &IDMWindow::resumeSelected);
    
    m_deleteSelectedAction = m_downloadMenu->addAction(QMainWindow::tr("&Delete Selected"));
    m_parentWindow->connect(m_deleteSelectedAction, &QAction::triggered, m_parentWindow, &IDMWindow::deleteSelected);
    
    m_downloadMenu->addSeparator();
    
    m_scheduleDownloadAction = m_downloadMenu->addAction(QMainWindow::tr("&Schedule Download..."));
    m_parentWindow->connect(m_scheduleDownloadAction, &QAction::triggered, m_parentWindow, &IDMWindow::scheduleDownload);
    
    m_downloadMenu->addSeparator();
    
    m_refreshAction = m_downloadMenu->addAction(QMainWindow::tr("&Refresh"));
    m_refreshAction->setShortcut(QMainWindow::tr("F5"));
    m_parentWindow->connect(m_refreshAction, &QAction::triggered, m_parentWindow, &IDMWindow::refreshList);
    
    m_downloadMenu->addSeparator();
    
    m_organizeDownloadsAction = m_downloadMenu->addAction(QMainWindow::tr("&Organize Downloads"));
    m_parentWindow->connect(m_organizeDownloadsAction, &QAction::triggered, m_parentWindow, &IDMWindow::organizeDownloads);
    
    m_deleteCompletedAction = m_downloadMenu->addAction(QMainWindow::tr("Delete C&ompleted"));
    m_parentWindow->connect(m_deleteCompletedAction, &QAction::triggered, m_parentWindow, &IDMWindow::deleteCompleted);

    // 工具菜单
    m_toolsMenu = m_menuBar->addMenu(QMainWindow::tr("&Tools"));
    m_optionsAction = m_toolsMenu->addAction(QMainWindow::tr("&Options..."));
    m_parentWindow->connect(m_optionsAction, &QAction::triggered, m_parentWindow, &IDMWindow::options);
    
    m_toolsMenu->addSeparator();
    
    m_connectToProxyAction = m_toolsMenu->addAction(QMainWindow::tr("Connect to Proxy"));
    m_parentWindow->connect(m_connectToProxyAction, &QAction::triggered, m_parentWindow, &IDMWindow::connectToProxy);
    
    m_disconnectFromProxyAction = m_toolsMenu->addAction(QMainWindow::tr("Disconnect from Proxy"));
    m_parentWindow->connect(m_disconnectFromProxyAction, &QAction::triggered, m_parentWindow, &IDMWindow::disconnectFromProxy);
    
    m_toolsMenu->addSeparator();
    
    m_readAloudAction = m_toolsMenu->addAction(QMainWindow::tr("Read Aloud"));
    m_readAloudAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_MediaVolume));
    m_parentWindow->connect(m_readAloudAction, &QAction::triggered, m_parentWindow, &IDMWindow::readAloud);

    // 帮助菜单
    m_helpMenu = m_menuBar->addMenu(QMainWindow::tr("&Help"));
    m_helpContentsAction = m_helpMenu->addAction(QMainWindow::tr("&Help Contents"));
    m_parentWindow->connect(m_helpContentsAction, &QAction::triggered, m_parentWindow, &IDMWindow::helpContents);
    
    m_helpMenu->addSeparator();
    
    m_aboutAction = m_helpMenu->addAction(QMainWindow::tr("&About"));
    m_parentWindow->connect(m_aboutAction, &QAction::triggered, m_parentWindow, &IDMWindow::about);
    
    // 视图菜单
    m_viewMenu = m_menuBar->addMenu(QMainWindow::tr("&View"));
    m_toggleThemeAction = m_viewMenu->addAction(QMainWindow::tr("Toggle Theme"));
    m_toggleThemeAction->setShortcut(QMainWindow::tr("Ctrl+T"));
    m_parentWindow->connect(m_toggleThemeAction, &QAction::triggered, m_parentWindow, &IDMWindow::toggleTheme);
}

void UIManager::createToolBars()
{
    // 创建主工具栏
    m_mainToolBar = m_parentWindow->addToolBar(QMainWindow::tr("Main"));
    m_mainToolBar->setObjectName("mainToolBar");
    m_mainToolBar->setMovable(false);
    m_mainToolBar->setIconSize(QSize(24, 24));
    m_mainToolBar->setStyleSheet(
        "QToolBar { "
        "  spacing: 5px; "
        "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #f0f0f0, stop: 1 #d0d0d0); "
        "  border-bottom: 1px solid #a0a0a0; "
        "  padding: 2px; "
        "} "
        "QToolButton { "
        "  border: 1px solid transparent; "
        "  border-radius: 2px; "
        "  padding: 3px; "
        "} "
        "QToolButton:hover { "
        "  border: 1px solid #808080; "
        "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e0e0e0, stop: 1 #c0c0c0); "
        "} "
        "QToolButton:pressed { "
        "  border: 1px solid #606060; "
        "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #c0c0c0, stop: 1 #e0e0e0); "
        "}"
    );
    
    setupToolBarActions();
}

void UIManager::setupToolBarActions()
{
    // 清除现有动作
    m_mainToolBar->clear();
    
    m_addUrlToolAction = m_mainToolBar->addAction(QMainWindow::tr("Add URL"));
    m_addUrlToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_FileDialogNewFolder));
    m_parentWindow->connect(m_addUrlToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::addUrl);
    
    m_mainToolBar->addSeparator();
    
    m_pauseToolAction = m_mainToolBar->addAction(QMainWindow::tr("Pause"));
    m_pauseToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_MediaPause));
    m_parentWindow->connect(m_pauseToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::pauseSelected);
    
    m_resumeToolAction = m_mainToolBar->addAction(QMainWindow::tr("Resume"));
    m_resumeToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_MediaPlay));
    m_parentWindow->connect(m_resumeToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::resumeSelected);
    
    m_deleteToolAction = m_mainToolBar->addAction(QMainWindow::tr("Delete"));
    m_deleteToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_TrashIcon));
    m_parentWindow->connect(m_deleteToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::deleteSelected);
    
    m_mainToolBar->addSeparator();
    
    m_scheduleToolAction = m_mainToolBar->addAction(QMainWindow::tr("Schedule"));
    m_scheduleToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_ToolBarHorizontalExtensionButton));
    m_parentWindow->connect(m_scheduleToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::scheduleDownload);
    
    m_mainToolBar->addSeparator();
    
    m_openFileToolAction = m_mainToolBar->addAction(QMainWindow::tr("Open File"));
    m_openFileToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_DialogOpenButton));
    m_parentWindow->connect(m_openFileToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::openFile);
    
    m_openFolderToolAction = m_mainToolBar->addAction(QMainWindow::tr("Open Folder"));
    m_openFolderToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_DirOpenIcon));
    m_parentWindow->connect(m_openFolderToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::openFolder);
    
    m_mainToolBar->addSeparator();
    
    m_propertiesToolAction = m_mainToolBar->addAction(QMainWindow::tr("Properties"));
    m_propertiesToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_FileDialogDetailedView));
    m_parentWindow->connect(m_propertiesToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::showProperties);
    
    m_mainToolBar->addSeparator();
    
    m_organizeToolAction = m_mainToolBar->addAction(QMainWindow::tr("Organize"));
    m_organizeToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_FileDialogContentsView));
    m_parentWindow->connect(m_organizeToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::organizeDownloads);
    
    m_deleteCompletedToolAction = m_mainToolBar->addAction(QMainWindow::tr("Delete Completed"));
    m_deleteCompletedToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_DialogDiscardButton));
    m_parentWindow->connect(m_deleteCompletedToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::deleteCompleted);
    
    m_mainToolBar->addSeparator();
    
    m_refreshToolAction = m_mainToolBar->addAction(QMainWindow::tr("Refresh"));
    m_refreshToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_BrowserReload));
    m_parentWindow->connect(m_refreshToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::refreshList);
    
    m_mainToolBar->addSeparator();
    
    m_toggleCategoryPanelAction = m_mainToolBar->addAction(QMainWindow::tr("Toggle Panel"));
    m_toggleCategoryPanelAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_TitleBarUnshadeButton));
    m_toggleCategoryPanelAction->setCheckable(true);
    m_toggleCategoryPanelAction->setChecked(true);
    m_parentWindow->connect(m_toggleCategoryPanelAction, &QAction::triggered, m_parentWindow, &IDMWindow::toggleCategoryPanel);
    
    m_mainToolBar->addSeparator();
    
    m_readAloudToolAction = m_mainToolBar->addAction(QMainWindow::tr("Read Aloud"));
    m_readAloudToolAction->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_MediaVolume));
    m_parentWindow->connect(m_readAloudToolAction, &QAction::triggered, m_parentWindow, &IDMWindow::readAloud);
}

void UIManager::createMainWidgets()
{
    // 创建标签页
    m_tabWidget = new QTabWidget(m_parentWindow);
    m_tabWidget->setStyleSheet(
        "QTabWidget::pane { "
        "  border: 1px solid #C0C0C0; "
        "} "
        "QTabBar::tab { "
        "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #f0f0f0, stop: 1 #d0d0d0); "
        "  border: 1px solid #a0a0a0; "
        "  border-bottom-color: #C0C0C0; "
        "  border-top-left-radius: 4px; "
        "  border-top-right-radius: 4px; "
        "  min-width: 8ex; "
        "  padding: 5px; "
        "} "
        "QTabBar::tab:selected { "
        "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffffff, stop: 1 #e0e0e0); "
        "  border-bottom-color: #ffffff; "
        "} "
        "QTabBar::tab:hover:!selected { "
        "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e8e8e8, stop: 1 #d8d8d8); "
        "}"
    );
    
    // 下载标签页
    m_downloadsTab = new QWidget();
    m_downloadsTab->setStyleSheet(
        "QWidget { "
        "  background-color: #f5f5f5; "
        "}"
    );
    
    QVBoxLayout *downloadsLayout = new QVBoxLayout(m_downloadsTab);
    downloadsLayout->setSpacing(5);
    downloadsLayout->setContentsMargins(5, 5, 5, 5);
    
    // 创建分割器
    QSplitter *splitter = new QSplitter(m_downloadsTab);
    splitter->setHandleWidth(1);
    splitter->setStyleSheet("QSplitter::handle { background-color: #C0C0C0; }");
    
    // 左侧类别面板
    m_categoryPanel = new QFrame();
    m_categoryPanel->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
    m_categoryPanel->setStyleSheet(
        "QFrame { "
        "  background-color: #f0f0f0; "
        "  border: 1px solid #C0C0C0; "
        "}"
    );
    
    QVBoxLayout *categoryLayout = new QVBoxLayout(m_categoryPanel);
    categoryLayout->setSpacing(2);
    categoryLayout->setContentsMargins(0, 0, 0, 0);
    
    // 添加标题
    QWidget *categoryHeader = new QWidget();
    categoryHeader->setObjectName("categoryHeader");
    categoryHeader->setStyleSheet(
        "QWidget { "
        "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e0e0e0, stop: 1 #d0d0d0); "
        "  border-bottom: 1px solid #C0C0C0; "
        "  padding: 4px; "
        "}"
    );
    
    QHBoxLayout *categoryHeaderLayout = new QHBoxLayout(categoryHeader);
    categoryHeaderLayout->setContentsMargins(5, 5, 5, 5);
    QLabel *categoriesLabel = new QLabel(QMainWindow::tr("Categories"));
    categoriesLabel->setStyleSheet("font-weight: bold; color: #000000;");
    categoryHeaderLayout->addWidget(categoriesLabel);
    categoryHeaderLayout->addStretch();
    
    m_categoryTree = new QTreeWidget();
    m_categoryTree->setHeaderHidden(true);
    m_categoryTree->setStyleSheet(
        "QTreeWidget { "
        "  border: none; "
        "  background-color: #f8f8f8; "
        "  color: #000000; "
        "} "
        "QTreeWidget::item { "
        "  padding: 3px; "
        "} "
        "QTreeWidget::item:selected { "
        "  background-color: #0078D7; "
        "  color: white; "
        "} "
        "QTreeWidget::item:hover { "
        "  background-color: #d0d0d0; "
        "}"
    );
    
    // 添加类别项
    QTreeWidgetItem *allItem = new QTreeWidgetItem(m_categoryTree);
    allItem->setText(0, QMainWindow::tr("All Downloads"));
    allItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_DirIcon));
    
    QTreeWidgetItem *uncategorizedItem = new QTreeWidgetItem(m_categoryTree);
    uncategorizedItem->setText(0, QMainWindow::tr("Uncategorized"));
    uncategorizedItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_DirClosedIcon));
    
    QTreeWidgetItem *fileTypesItem = new QTreeWidgetItem(m_categoryTree);
    fileTypesItem->setText(0, QMainWindow::tr("File Types"));
    fileTypesItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_DirLinkIcon));
    
    QTreeWidgetItem *audioItem = new QTreeWidgetItem(fileTypesItem);
    audioItem->setText(0, QMainWindow::tr("Audio"));
    audioItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_MediaVolume));
    
    QTreeWidgetItem *videoItem = new QTreeWidgetItem(fileTypesItem);
    videoItem->setText(0, QMainWindow::tr("Video"));
    videoItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_MediaPlay));
    
    QTreeWidgetItem *imageItem = new QTreeWidgetItem(fileTypesItem);
    imageItem->setText(0, QMainWindow::tr("Image"));
    imageItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_FileDialogContentsView));
    
    QTreeWidgetItem *compressedItem = new QTreeWidgetItem(fileTypesItem);
    compressedItem->setText(0, QMainWindow::tr("Compressed"));
    compressedItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_DriveHDIcon));
    
    QTreeWidgetItem *documentsItem = new QTreeWidgetItem(fileTypesItem);
    documentsItem->setText(0, QMainWindow::tr("Documents"));
    documentsItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_FileDialogDetailedView));
    
    QTreeWidgetItem *programsItem = new QTreeWidgetItem(fileTypesItem);
    programsItem->setText(0, QMainWindow::tr("Programs"));
    programsItem->setIcon(0, m_parentWindow->style()->standardIcon(QStyle::SP_DialogOpenButton));
    
    fileTypesItem->setExpanded(true);
    
    // 搜索和过滤区域
    QFrame *searchFrame = new QFrame();
    searchFrame->setObjectName("searchFrame");
    searchFrame->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
    searchFrame->setStyleSheet(
        "QFrame { "
        "  background-color: #e8e8e8; "
        "  border: 1px solid #C0C0C0; "
        "  border-radius: 3px; "
        "  padding: 5px; "
        "}"
    );
    
    QHBoxLayout *searchLayout = new QHBoxLayout(searchFrame);
    searchLayout->setSpacing(5);
    searchLayout->setContentsMargins(5, 5, 5, 5);
    
    m_searchBox = new QLineEdit();
    m_searchBox->setPlaceholderText(QMainWindow::tr("Search downloads..."));
    m_searchBox->setMaximumWidth(150);
    m_searchBox->setStyleSheet(
        "QLineEdit { "
        "  padding: 3px; "
        "  border: 1px solid #C0C0C0; "
        "  border-radius: 2px; "
        "}"
    );
    
    m_filterComboBox = new QComboBox();
    m_filterComboBox->addItem(QMainWindow::tr("All"));
    m_filterComboBox->addItem(QMainWindow::tr("Active"));
    m_filterComboBox->addItem(QMainWindow::tr("Completed"));
    m_filterComboBox->addItem(QMainWindow::tr("Paused"));
    m_filterComboBox->addItem(QMainWindow::tr("Error"));
    m_filterComboBox->setMaximumWidth(100);
    m_filterComboBox->setStyleSheet(
        "QComboBox { "
        "  padding: 3px; "
        "  border: 1px solid #C0C0C0; "
        "  border-radius: 2px; "
        "} "
        "QComboBox::drop-down { "
        "  subcontrol-origin: padding; "
        "  subcontrol-position: top right; "
        "  width: 20px; "
        "  border-left-width: 1px; "
        "  border-left-color: #C0C0C0; "
        "  border-left-style: solid; "
        "} "
        "QComboBox QAbstractItemView { "
        "}"
    );
    
    searchLayout->addWidget(new QLabel(QMainWindow::tr("Filter:")));
    searchLayout->addWidget(m_filterComboBox);
    searchLayout->addWidget(new QLabel(QMainWindow::tr("Search:")));
    searchLayout->addWidget(m_searchBox);
    searchLayout->addStretch();
    
    categoryLayout->addWidget(categoryHeader);
    categoryLayout->addWidget(m_categoryTree);
    categoryLayout->addWidget(searchFrame);
    
    // 右侧下载列表
    QWidget *rightPanel = new QWidget();
    rightPanel->setStyleSheet(
        "QWidget { "
        "  background-color: #f5f5f5; "
        "}"
    );
    
    QVBoxLayout *rightLayout = new QVBoxLayout(rightPanel);
    rightLayout->setSpacing(2);
    rightLayout->setContentsMargins(0, 0, 0, 0);
    
    // 添加标题
    QWidget *downloadsHeader = new QWidget();
    downloadsHeader->setObjectName("downloadsHeader");
    downloadsHeader->setStyleSheet(
        "QWidget { "
        "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e0e0e0, stop: 1 #d0d0d0); "
        "  border: 1px solid #C0C0C0; "
        "  border-bottom: none; "
        "  padding: 4px; "
        "}"
    );
    
    QHBoxLayout *downloadsHeaderLayout = new QHBoxLayout(downloadsHeader);
    downloadsHeaderLayout->setContentsMargins(5, 5, 5, 5);
    QLabel *downloadsLabel = new QLabel(QMainWindow::tr("Downloads"));
    downloadsLabel->setStyleSheet("font-weight: bold; color: #000000;");
    downloadsHeaderLayout->addWidget(downloadsLabel);
    downloadsHeaderLayout->addStretch();
    
    m_downloadList = new QTableWidget(0, 9, m_parentWindow);
    m_downloadList->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_downloadList->setSelectionMode(QAbstractItemView::ExtendedSelection);
    m_downloadList->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_downloadList->setAlternatingRowColors(true);
    m_downloadList->setStyleSheet(
        "QTableWidget { "
        "  gridline-color: #d0d0d0; "
        "  border: 1px solid #C0C0C0; "
        "  background-color: #ffffff; "
        "  color: #000000; "
        "} "
        "QTableWidget::item { "
        "  color: #000000; "
        "} "
        "QTableWidget::item:selected { "
        "  background-color: #0078D7; "
        "  color: white; "
        "} "
        "QTableWidget::item:hover { "
        "  background-color: #e0e0e0; "
        "} "
        "QHeaderView::section { "
        "  background-color: #e8e8e8; "
        "  padding: 4px; "
        "  border: 1px solid #C0C0C0; "
        "  font-weight: bold; "
        "  color: #000000; "
        "} "
        "QTableCornerButton::section { "
        "  background: #e8e8e8; "
        "  border: 1px solid #C0C0C0; "
        "} "
        "QTableWidget QTableCornerButton::section { "
        "  background: #e8e8e8; "
        "  border: 1px solid #C0C0C0; "
        "}"
    );
    
    // 设置表头
    QStringList headers;
    headers << QMainWindow::tr("#") << QMainWindow::tr("File") << QMainWindow::tr("URL") << QMainWindow::tr("Progress") << QMainWindow::tr("Speed") 
            << QMainWindow::tr("Size") << QMainWindow::tr("Status") << QMainWindow::tr("Time Left") << QMainWindow::tr("Added");
    m_downloadList->setHorizontalHeaderLabels(headers);
    
    // 设置表头属性
    QHeaderView *header = m_downloadList->horizontalHeader();
    header->setSectionResizeMode(QHeaderView::Interactive);
    header->setStretchLastSection(true);
    
    // 设置交替行颜色
    m_downloadList->setAlternatingRowColors(true);
    QPalette palette = m_downloadList->palette();
    palette.setColor(QPalette::AlternateBase, QColor(240, 240, 240));
    m_downloadList->setPalette(palette);
    
    // 设置列宽
    m_downloadList->setColumnWidth(0, 30);   // #
    m_downloadList->setColumnWidth(1, 180);  // File
    m_downloadList->setColumnWidth(2, 200);  // URL
    m_downloadList->setColumnWidth(3, 150);  // Progress
    m_downloadList->setColumnWidth(4, 90);   // Speed
    m_downloadList->setColumnWidth(5, 80);   // Size
    m_downloadList->setColumnWidth(6, 100);  // Status
    m_downloadList->setColumnWidth(7, 90);   // Time Left
    m_downloadList->setColumnWidth(8, 120);  // Added
    
    // 添加示例行数据
    m_downloadList->insertRow(0);
    m_downloadList->setItem(0, 0, new QTableWidgetItem("1"));
    m_downloadList->setItem(0, 1, new QTableWidgetItem(QMainWindow::tr("example.zip")));
    m_downloadList->setItem(0, 2, new QTableWidgetItem(QMainWindow::tr("https://example.com/files/example.zip")));
    m_downloadList->setItem(0, 3, new QTableWidgetItem(QString("")));
    m_downloadList->setItem(0, 4, new QTableWidgetItem(QMainWindow::tr("2.5 MB/s")));
    m_downloadList->setItem(0, 5, new QTableWidgetItem(QMainWindow::tr("15.2 MB")));
    m_downloadList->setItem(0, 6, new QTableWidgetItem(QMainWindow::tr("Downloading")));
    m_downloadList->setItem(0, 7, new QTableWidgetItem(QMainWindow::tr("00:03:15")));
    m_downloadList->setItem(0, 8, new QTableWidgetItem(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm")));
    
    // 在进度列中添加进度条
    QProgressBar *progressBar = new QProgressBar();
    progressBar->setValue(45);
    progressBar->setStyleSheet(
        "QProgressBar { "
        "  border: 1px solid grey; "
        "  border-radius: 2px; "
        "  text-align: center; "
        "  height: 15px; "
        "} "
        "QProgressBar::chunk { "
        "  background-color: #05B8CC; "
        "  width: 10px; "
        "}"
    );
    m_downloadList->setCellWidget(0, 3, progressBar);
    
    // 添加第二个示例行
    m_downloadList->insertRow(1);
    m_downloadList->setItem(1, 0, new QTableWidgetItem("2"));
    m_downloadList->setItem(1, 1, new QTableWidgetItem(QMainWindow::tr("document.pdf")));
    m_downloadList->setItem(1, 2, new QTableWidgetItem(QMainWindow::tr("https://example.com/docs/document.pdf")));
    m_downloadList->setItem(1, 3, new QTableWidgetItem(QString("")));
    m_downloadList->setItem(1, 4, new QTableWidgetItem(QMainWindow::tr("1.2 MB/s")));
    m_downloadList->setItem(1, 5, new QTableWidgetItem(QMainWindow::tr("5.7 MB")));
    m_downloadList->setItem(1, 6, new QTableWidgetItem(QMainWindow::tr("Completed")));
    m_downloadList->setItem(1, 7, new QTableWidgetItem(QMainWindow::tr("")));
    m_downloadList->setItem(1, 8, new QTableWidgetItem(QDateTime::currentDateTime().addDays(-1).toString("yyyy-MM-dd hh:mm")));
    
    QProgressBar *progressBar2 = new QProgressBar();
    progressBar2->setValue(100);
    progressBar2->setStyleSheet(
        "QProgressBar { "
        "  border: 1px solid grey; "
        "  border-radius: 2px; "
        "  text-align: center; "
        "  height: 15px; "
        "} "
        "QProgressBar::chunk { "
        "  background-color: #4CAF50; "
        "  width: 10px; "
        "}"
    );
    m_downloadList->setCellWidget(1, 3, progressBar2);
    
    // 添加右键菜单
    m_downloadList->setContextMenuPolicy(Qt::CustomContextMenu);
    m_parentWindow->connect(m_downloadList, &QWidget::customContextMenuRequested, m_parentWindow, &IDMWindow::showContextMenu);
    
    // 确保表格始终显示固定行数（即使没有数据也要显示表格结构）
    const int minRowCount = 30; // 最小显示行数
    int currentRowCount = m_downloadList->rowCount();
    
    // 如果当前行数少于最小行数，则添加空行
    for (int i = currentRowCount; i < minRowCount; ++i) {
        m_downloadList->insertRow(i);
        
        // 为每一列添加空的 QTableWidgetItem，确保表格线显示
        for (int col = 0; col < m_downloadList->columnCount(); ++col) {
            QTableWidgetItem *item = new QTableWidgetItem("");
            item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 设置为不可编辑
            m_downloadList->setItem(i, col, item);
        }
    }
    
    rightLayout->addWidget(downloadsHeader);
    rightLayout->addWidget(m_downloadList);
    
    // 将部件添加到分割器
    splitter->addWidget(m_categoryPanel);
    splitter->addWidget(rightPanel);
    splitter->setSizes(QList<int>() << 200 << 900);
    
    downloadsLayout->addWidget(splitter);
    
    // 其他标签页（简化实现）
    m_scheduleTab = new QWidget();
    m_scheduleTab->setStyleSheet("QWidget { background-color: #f5f5f5; }");
    QVBoxLayout *scheduleLayout = new QVBoxLayout(m_scheduleTab);
    QLabel *scheduleLabel = new QLabel(QMainWindow::tr("Scheduled Downloads - This feature will be implemented in future versions"));
    scheduleLabel->setAlignment(Qt::AlignCenter);
    scheduleLabel->setStyleSheet("QLabel { font-size: 14px; color: #333333; }");
    scheduleLayout->addWidget(scheduleLabel);
    
    m_youTubeTab = new QWidget();
    m_youTubeTab->setStyleSheet("QWidget { background-color: #f5f5f5; }");
    QVBoxLayout *youtubeLayout = new QVBoxLayout(m_youTubeTab);
    QLabel *youtubeLabel = new QLabel(QMainWindow::tr("YouTube Downloader - This feature will be implemented in future versions"));
    youtubeLabel->setAlignment(Qt::AlignCenter);
    youtubeLabel->setStyleSheet("QLabel { font-size: 14px; color: #333333; }");
    youtubeLayout->addWidget(youtubeLabel);
    
    m_grabberTab = new QWidget();
    m_grabberTab->setStyleSheet("QWidget { background-color: #f5f5f5; }");
    QVBoxLayout *grabberLayout = new QVBoxLayout(m_grabberTab);
    QLabel *grabberLabel = new QLabel(QMainWindow::tr("Site Grabber - This feature will be implemented in future versions"));
    grabberLabel->setAlignment(Qt::AlignCenter);
    grabberLabel->setStyleSheet("QLabel { font-size: 14px; color: #333333; }");
    grabberLayout->addWidget(grabberLabel);
    
    // 添加标签页
    m_tabWidget->addTab(m_downloadsTab, QMainWindow::tr("Downloads"));
    m_tabWidget->addTab(m_scheduleTab, QMainWindow::tr("Scheduler"));
    m_tabWidget->addTab(m_youTubeTab, QMainWindow::tr("YouTube"));
    m_tabWidget->addTab(m_grabberTab, QMainWindow::tr("Grabber"));
}

void UIManager::createStatusBar()
{
    m_mainStatusBar = m_parentWindow->statusBar();
    m_mainStatusBar->setStyleSheet("QStatusBar { border-top: 1px solid #C0C0C0; }");
    
    m_statusLabel = new QLabel(QMainWindow::tr("Ready"));
    m_statusLabel->setStyleSheet("QLabel { padding: 2px; }");
    m_mainStatusBar->addWidget(m_statusLabel, 1);
    
    m_activeDownloadsLabel = new QLabel(QMainWindow::tr("Active: 1"));
    m_activeDownloadsLabel->setStyleSheet("QLabel { padding: 2px; }");
    m_mainStatusBar->addPermanentWidget(m_activeDownloadsLabel);
    
    m_speedLabel = new QLabel(QMainWindow::tr("Speed: 2.5 MB/s"));
    m_speedLabel->setStyleSheet("QLabel { padding: 2px; }");
    m_mainStatusBar->addPermanentWidget(m_speedLabel);
    
    m_downloadProgressBar = new QProgressBar();
    m_downloadProgressBar->setRange(0, 100);
    m_downloadProgressBar->setValue(45);
    m_downloadProgressBar->setFixedWidth(150);
    m_downloadProgressBar->setStyleSheet(
        "QProgressBar { "
        "  border: 1px solid grey; "
        "  border-radius: 2px; "
        "  text-align: center; "
        "} "
        "QProgressBar::chunk { "
        "  background-color: #05B8CC; "
        "  width: 10px; "
        "}"
    );
    m_mainStatusBar->addPermanentWidget(m_downloadProgressBar);
}

void UIManager::createSystemTrayIcon()
{
    m_trayIcon = new QSystemTrayIcon(m_parentWindow);
    m_trayIcon->setIcon(m_parentWindow->style()->standardIcon(QStyle::SP_ComputerIcon));
    
    m_trayIconMenu = new QMenu(m_parentWindow);
    m_trayIconMenu->addAction(QMainWindow::tr("Open IDM"), m_parentWindow, SLOT(show()));
    m_trayIconMenu->addAction(QMainWindow::tr("Exit"), m_parentWindow, SLOT(exitApplication()));
    
    m_trayIcon->setContextMenu(m_trayIconMenu);
    m_trayIcon->setToolTip(QMainWindow::tr("Internet Download Manager"));
    
    m_parentWindow->connect(m_trayIcon, &QSystemTrayIcon::activated, m_parentWindow, &IDMWindow::trayIconActivated);
    
    m_trayIcon->show();
}

void UIManager::setupConnections()
{
    // 连接搜索框信号
    m_parentWindow->connect(m_searchBox, &QLineEdit::textChanged, [this](const QString &text) {
        // 实现搜索功能的简化版本
        for (int row = 0; row < m_downloadList->rowCount(); ++row) {
            bool match = text.isEmpty();
            if (!match) {
                QTableWidgetItem *item = m_downloadList->item(row, 1); // 文件名列
                if (item && item->text().contains(text, Qt::CaseInsensitive)) {
                    match = true;
                }
            }
            m_downloadList->setRowHidden(row, !match);
        }
    });
    
    // 连接过滤器信号
    m_parentWindow->connect(m_filterComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), m_parentWindow, &IDMWindow::filterDownloads);
}

void UIManager::applyTheme()
{
    ThemeManager* themeManager = ThemeManager::instance();
    if (themeManager->currentTheme() == ThemeManager::Dark) {
        // 应用深色主题样式
        QString styleSheet = R"(
            QMainWindow, QWidget {
                color: #ffffff;
                background-color: #2d2d2d;
            }
            QMenuBar {
                background-color: #333333;
                color: #ffffff;
            }
            QMenuBar::item {
                background: transparent;
                color: #ffffff;
            }
            QMenuBar::item:selected {
                background: #3a3a3a;
                color: #ffffff;
            }
            QMenuBar::item:pressed {
                background: #454545;
                color: #ffffff;
            }
            QMenu {
                background-color: #333333;
                color: #ffffff;
                border: 1px solid #444444;
            }
            QMenu::item {
                padding: 5px 20px;
                color: #ffffff;
            }
            QMenu::item:selected {
                background-color: #4d4d4d;
                color: #ffffff;
            }
            QToolBar {
                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #3a3a3a, stop: 1 #2a2a2a);
                border-bottom: 1px solid #555555;
                color: #ffffff;
            }
            QToolButton {
                border: 1px solid transparent;
                border-radius: 2px;
                padding: 3px;
                color: #ffffff;
                background: transparent;
            }
            QToolButton:hover {
                border: 1px solid #666666;
                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #4a4a4a, stop: 1 #3a3a3a);
                color: #ffffff;
            }
            QToolButton:pressed {
                border: 1px solid #777777;
                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #3a3a3a, stop: 1 #4a4a4a);
                color: #ffffff;
            }
            QTreeWidget, QTableWidget {
                background-color: #1e1e1e;
                alternate-background-color: #2d2d2d;
                color: #ffffff;
                selection-background-color: #2a5a8a;
                selection-color: #ffffff;
                gridline-color: #444444;
                border: 1px solid #555555;
            }
            QTreeWidget::item, QTableWidget::item {
                color: #ffffff;
            }
            QHeaderView::section {
                background-color: #3a3a3a;
                color: #ffffff;
                padding: 4px;
                border: 1px solid #555555;
            }
            QFrame {
                background-color: #2d2d2d;
                border: 1px solid #555555;
                color: #ffffff;
            }
            QLineEdit, QComboBox {
                background-color: #1e1e1e;
                color: #ffffff;
                border: 1px solid #555555;
                selection-background-color: #2a5a8a;
            }
            QProgressBar {
                border: 1px solid #555555;
                border-radius: 2px;
                background-color: #1e1e1e;
                color: #ffffff;
            }
            QProgressBar::chunk {
                background-color: #05B8CC;
            }
            QStatusBar {
                background-color: #333333;
                color: #ffffff;
                border-top: 1px solid #555555;
            }
            QPushButton {
                background-color: #3a3a3a;
                color: #ffffff;
                border: 1px solid #555555;
                padding: 5px;
                border-radius: 2px;
            }
            QPushButton:hover {
                background-color: #4a4a4a;
                color: #ffffff;
            }
            QPushButton:pressed {
                background-color: #2a2a2a;
                color: #ffffff;
            }
            QScrollBar:vertical {
                background-color: #2d2d2d;
                width: 15px;
                border: 1px solid #555555;
            }
            QScrollBar::handle:vertical {
                background-color: #555555;
                min-height: 20px;
            }
            QScrollBar::handle:vertical:hover {
                background-color: #666666;
            }
            QLabel {
                color: #ffffff;
            }
        )";
        qApp->setStyleSheet(styleSheet);
    } else {
        // 应用浅色主题样式（默认）
        QString styleSheet = R"(
            QMainWindow, QWidget {
                color: #000000;
                background-color: #f0f0f0;
            }
            QMenuBar {
                background-color: #f0f0f0;
                color: #000000;
            }
            QMenuBar::item {
                background: transparent;
                color: #000000;
            }
            QMenuBar::item:selected {
                background: #d0d0d0;
                color: #000000;
            }
            QMenuBar::item:pressed {
                background: #c0c0c0;
                color: #000000;
            }
            QMenu {
                background-color: #ffffff;
                color: #000000;
                border: 1px solid #c0c0c0;
            }
            QMenu::item {
                padding: 5px 20px;
                color: #000000;
            }
            QMenu::item:selected {
                background-color: #0078D7;
                color: #ffffff;
            }
            QToolBar {
                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #f0f0f0, stop: 1 #d0d0d0);
                border-bottom: 1px solid #a0a0a0;
            }
            QToolButton {
                border: 1px solid transparent;
                border-radius: 2px;
                padding: 3px;
                color: #000000;
            }
            QToolButton:hover {
                border: 1px solid #808080;
                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e0e0e0, stop: 1 #c0c0c0);
            }
            QToolButton:pressed {
                border: 1px solid #606060;
                background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #c0c0c0, stop: 1 #e0e0e0);
            }
        )";
        qApp->setStyleSheet(styleSheet);
    }

    // 应用分类栏和列表样式
    applyCategoryAndListStyles();
}

void UIManager::applyCategoryAndListStyles()
{
    ThemeManager* themeManager = ThemeManager::instance();
    if (themeManager->currentTheme() == ThemeManager::Dark) {
        // 深色主题样式
        m_categoryPanel->setStyleSheet(
            "QFrame { "
            "  background-color: #2d2d2d; "
            "  border: 1px solid #555555; "
            "}"
        );
        
        // 分类面板标题
        // 注意：我们需要找到categoryHeader并更新它的样式
        QWidget* categoryHeader = m_categoryPanel->findChild<QWidget*>("categoryHeader");
        if (categoryHeader) {
            categoryHeader->setStyleSheet(
                "QWidget#categoryHeader { "
                "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #3a3a3a, stop: 1 #2a2a2a); "
                "  border-bottom: 1px solid #555555; "
                "  padding: 4px; "
                "  color: #ffffff; "
                "}"
            );
            
            // 更新标题标签的颜色
            QLabel* categoriesLabel = categoryHeader->findChild<QLabel*>();
            if (categoriesLabel) {
                categoriesLabel->setStyleSheet("font-weight: bold; color: #ffffff;");
            }
        }
        
        // 分类树
        m_categoryTree->setStyleSheet(
            "QTreeWidget { "
            "  border: none; "
            "  background-color: #1e1e1e; "
            "  color: #ffffff; "
            "} "
            "QTreeWidget::item { "
            "  padding: 3px; "
            "  color: #ffffff; "
            "} "
            "QTreeWidget::item:selected { "
            "  background-color: #2a5a8a; "
            "  color: #ffffff; "
            "} "
            "QTreeWidget::item:hover { "
            "  background-color: #3a3a3a; "
            "  color: #ffffff; "
            "}"
        );
        
        // 搜索框区域
        QWidget* searchFrame = m_parentWindow->findChild<QWidget*>("searchFrame");
        if (searchFrame) {
            searchFrame->setStyleSheet(
                "QWidget { "
                "  background-color: #2d2d2d; "
                "  border: 1px solid #555555; "
                "  border-radius: 3px; "
                "  padding: 5px; "
                "}"
            );
        }
        
        // 右侧面板
        // downloadsTab的样式已在全局样式表中设置
        
        // 下载列表标题
        QWidget* downloadsHeader = m_parentWindow->findChild<QWidget*>("downloadsHeader");
        if (downloadsHeader) {
            downloadsHeader->setStyleSheet(
                "QWidget { "
                "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #3a3a3a, stop: 1 #2a2a2a); "
                "  border: 1px solid #555555; "
                "  border-bottom: none; "
                "  padding: 4px; "
                "}"
            );
        }
        
        m_downloadList->setStyleSheet(
            "QTableWidget { "
            "  gridline-color: #444444; "
            "  border: 1px solid #555555; "
            "  background-color: #1e1e1e; "
            "  color: #ffffff; "
            "} "
            "QTableWidget::item { "
            "  color: #ffffff; "
            "} "
            "QTableWidget::item:selected { "
            "  background-color: #2a5a8a; "
            "  color: #ffffff; "
            "} "
            "QTableWidget::item:hover { "
            "  background-color: #3a3a3a; "
            "  color: #ffffff; "
            "} "
            "QHeaderView::section { "
            "  background-color: #3a3a3a; "
            "  color: #ffffff; "
            "  padding: 4px; "
            "  border: 1px solid #555555; "
            "} "
            "QTableCornerButton::section { "
            "  background: #3a3a3a; "
            "  border: 1px solid #555555; "
            "}"
        );
        
        // 更新工具栏样式
        m_mainToolBar->setStyleSheet(
            "QToolBar { "
            "  spacing: 5px; "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #3a3a3a, stop: 1 #2a2a2a); "
            "  border-bottom: 1px solid #555555; "
            "  padding: 2px; "
            "  color: #ffffff; "
            "} "
            "QToolButton { "
            "  border: 1px solid transparent; "
            "  border-radius: 2px; "
            "  padding: 3px; "
            "  color: #ffffff; "
            "  background: transparent; "
            "} "
            "QToolButton:hover { "
            "  border: 1px solid #666666; "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #4a4a4a, stop: 1 #3a3a3a); "
            "  color: #ffffff; "
            "} "
            "QToolButton:pressed { "
            "  border: 1px solid #777777; "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #3a3a3a, stop: 1 #4a4a4a); "
            "  color: #ffffff; "
            "}"
        );
        
        // 更新标签页样式
        m_tabWidget->setStyleSheet(
            "QTabWidget::pane { "
            "  border: 1px solid #555555; "
            "} "
            "QTabBar::tab { "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #3a3a3a, stop: 1 #2a2a2a); "
            "  border: 1px solid #555555; "
            "  border-bottom-color: #555555; "
            "  border-top-left-radius: 4px; "
            "  border-top-right-radius: 4px; "
            "  min-width: 8ex; "
            "  padding: 5px; "
            "  color: #ffffff; "
            "} "
            "QTabBar::tab:selected { "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #4a4a4a, stop: 1 #3a3a3a); "
            "  border-bottom-color: #2d2d2d; "
            "  color: #ffffff; "
            "} "
            "QTabBar::tab:hover:!selected { "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #454545, stop: 1 #353535); "
            "  color: #ffffff; "
            "}"
        );
    } else {
        // 浅色主题样式（恢复默认）
        m_categoryPanel->setStyleSheet(
            "QFrame { "
            "  background-color: #f0f0f0; "
            "  border: 1px solid #C0C0C0; "
            "}"
        );
        
        // 修复分类面板标题区域样式
        QWidget* categoryHeader = m_parentWindow->findChild<QWidget*>("categoryHeader");
        if (categoryHeader) {
            categoryHeader->setStyleSheet(
                "QWidget#categoryHeader { "
                "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e0e0e0, stop: 1 #d0d0d0); "
                "  border-bottom: 1px solid #C0C0C0; "
                "  padding: 4px; "
                "  color: #000000; "
                "}"
            );
            
            // 恢复标题标签的颜色
            QLabel* categoriesLabel = categoryHeader->findChild<QLabel*>();
            if (categoriesLabel) {
                categoriesLabel->setStyleSheet("font-weight: bold; color: #000000;");
            }
        }
        
        m_categoryTree->setStyleSheet(
            "QTreeWidget { "
            "  border: none; "
            "  background-color: #f8f8f8; "
            "  color: #000000; "
            "} "
            "QTreeWidget::item { "
            "  padding: 3px; "
            "} "
            "QTreeWidget::item:selected { "
            "  background-color: #0078D7; "
            "  color: white; "
            "} "
            "QTreeWidget::item:hover { "
            "  background-color: #d0d0d0; "
            "}"
        );
        
        QWidget* searchFrame = m_parentWindow->findChild<QWidget*>("searchFrame");
        if (searchFrame) {
            searchFrame->setStyleSheet(
                "QWidget { "
                "  background-color: #e8e8e8; "
                "  border: 1px solid #C0C0C0; "
                "  border-radius: 3px; "
                "  padding: 5px; "
                "}"
            );
        }
        
        QWidget* downloadsHeader = m_parentWindow->findChild<QWidget*>("downloadsHeader");
        if (downloadsHeader) {
            downloadsHeader->setStyleSheet(
                "QWidget { "
                "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e0e0e0, stop: 1 #d0d0d0); "
                "  border: 1px solid #C0C0C0; "
                "  border-bottom: none; "
                "  padding: 4px; "
                "}"
            );
        }
        
        m_downloadList->setStyleSheet(
            "QTableWidget { "
            "  gridline-color: #d0d0d0; "
            "  border: 1px solid #C0C0C0; "
            "  background-color: #ffffff; "
            "  color: #000000; "
            "} "
            "QTableWidget::item { "
            "  color: #000000; "
            "} "
            "QTableWidget::item:selected { "
            "  background-color: #0078D7; "
            "  color: white; "
            "} "
            "QTableWidget::item:hover { "
            "  background-color: #e0e0e0; "
            "} "
            "QHeaderView::section { "
            "  background-color: #e8e8e8; "
            "  padding: 4px; "
            "  border: 1px solid #C0C0C0; "
            "  color: #000000; "
            "} "
            "QTableCornerButton::section { "
            "  background: #e8e8e8; "
            "  border: 1px solid #C0C0C0; "
            "}"
        );
        
        // 更新工具栏样式
        m_mainToolBar->setStyleSheet(
            "QToolBar { "
            "  spacing: 5px; "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #f0f0f0, stop: 1 #d0d0d0); "
            "  border-bottom: 1px solid #a0a0a0; "
            "  padding: 2px; "
            "} "
            "QToolButton { "
            "  border: 1px solid transparent; "
            "  border-radius: 2px; "
            "  padding: 3px; "
            "} "
            "QToolButton:hover { "
            "  border: 1px solid #808080; "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e0e0e0, stop: 1 #c0c0c0); "
            "} "
            "QToolButton:pressed { "
            "  border: 1px solid #606060; "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #c0c0c0, stop: 1 #e0e0e0); "
            "}"
        );
        
        // 更新标签页样式
        m_tabWidget->setStyleSheet(
            "QTabWidget::pane { "
            "  border: 1px solid #C0C0C0; "
            "} "
            "QTabBar::tab { "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #f0f0f0, stop: 1 #d0d0d0); "
            "  border: 1px solid #a0a0a0; "
            "  border-bottom-color: #C0C0C0; "
            "  border-top-left-radius: 4px; "
            "  border-top-right-radius: 4px; "
            "  min-width: 8ex; "
            "  padding: 5px; "
            "  color: #000000; "
            "} "
            "QTabBar::tab:selected { "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffffff, stop: 1 #e0e0e0); "
            "  border-bottom-color: #ffffff; "
            "  color: #000000; "
            "} "
            "QTabBar::tab:hover:!selected { "
            "  background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e8e8e8, stop: 1 #d8d8d8); "
            "  color: #000000; "
            "}"
        );
    }
}